package ch.epfl.lara.pm.analyze.set;

import ch.epfl.lara.pm.ast.{PatternDef,VarPattern,ClassPattern}
import ch.epfl.lara.pm.global.ObjectTable
import ch.epfl.lara.pm.util.MyImplicitConversions

/**
 * Map the IR of patterns to an equivalent representation that contains some needed information to sucesfully
 * transform patterns into a list of set (that basically represent a cartesian product among sets).
 *
 * @pattern: The pattern being transformed
 * @return : The equivalent representation of the pattern gave in argument.
 */
object PatternConverter extends MyImplicitConversions {

  def apply(pattern: PatternDef): IPatternDef = { 
    def convertPattern(pattern: PatternDef, path: String): IPatternDef = pattern match {
      case VarPattern(_,tpe) => IVarPattern(tpe,path)
      case ClassPattern(objname,fields) => 
        val tpes = ObjectTable.getExtractorFieldsType(objname)
        val ifields = fields.zipWithIndex.map(pair => convertPattern(pair._1,path+objname+pair._2))
      
        IClassPattern(objname,
                      ifields.zip(tpes).map(pair =>IPatternRefined(pair._1,pair._2)),
                      path)
    }
    convertPattern(pattern,"")
  }
}
